<?php

namespace bc\model\knowledge;

use bc\ar\MActiveRecord;

class TagModel extends MActiveRecord
{
	public $autoId = 'tag_id';
	
	public static function tableName()
	{
		return '{{%knowledge_tag}}';
	}
	
	
	/**
	 *	查询数据表的信息
	 * @param array $params
	 * @param integer $page
	 * @param integer $pageSize
	 */
	public function getBackendList($params, $page = null, $pageSize = null)
	{   
            $sql = "select a.tag_id,a.tag_name,a.tag_type,b.catnm,c.catnm1
                    from bc_knowledge_tag a
                    left join
                    (select tag_id,'孕产' as catnm from bc_knowledge_category_tag where category_tag_status=1 and category_id=1) b on a.tag_id=b.tag_id
                    left join
                    (select tag_id,'育儿' as catnm1 from bc_knowledge_category_tag where category_tag_status=1 and category_id=2) c on a.tag_id=c.tag_id 
                    where {$params}";
            $sql .= " ORDER BY tag_id desc";
            $page = ($page - 1) * $pageSize;
            $sql .= " LIMIT {$page}, {$pageSize}";
            
//            echo $sql;
            $data = $this->getDb()->createCommand($sql)->queryAll();
//            $data = self::findBySql( $sql )->offset($page)->limit($pageSize)->asArray()->all();
//            print_r($sql);die();
            //echo "<pre>";
            //var_dump($data);die();
            return $data;
                
//            if ($page && $pageSize)
//		{
//			$offset = $this->getOffset ( $page, $pageSize );
//			$ar->offset($offset)->limit($pageSize);
//		}
	
		if($this->sort == null)
			$ar->orderBy ( $this->autoId . ' DESC' );
		else
			$ar->orderBy ( $this->sort );
		
		return $ar->asArray()->all();
                //echo $ar->createCommand()->getRawSql();die();

	}
	
	/**
	 *
	 * @param array $condition
	 * @return integer
	 */

        
        public function getBackendTotal($params, $page = null, $pageSize = null)
	{       

            $sql = "select count(*)
                    from bc_knowledge_tag a
                    left join
                    (select tag_id,'孕产' as catnm from bc_knowledge_category_tag where category_tag_status=1 and category_id=1) b on a.tag_id=b.tag_id
                    left join
                    (select tag_id,'育儿' as catnm1 from bc_knowledge_category_tag where category_tag_status=1 and category_id=2) c on a.tag_id=c.tag_id where {$params}";
            $count = $this->getDb()->createCommand($sql)->queryOne();
            foreach ($count as $value) {
                $count = $value;
            }
            return $count;
        }   
        
        
        public function getTagList($params)
    {    
                $this->columns = "tag_id,tag_name";
                $ar = $this->find ()->select($this->columns)->where($params);
	
		if($this->sort == null)
			$ar->orderBy ( $this->autoId . ' DESC' );
		else
			$ar->orderBy ( $this->sort );
		
		return $ar->asArray()->all();
    } 
        
}

